package main

import "fmt"

const PI = 3.14

type shaper interface {
	CalArea()
	GetArea() float64
}

type round struct {
	radius float64
	area   float64
}

func (r *round) CalArea() {
	area := PI * r.radius * r.radius
	r.area = area
}

func (r *round) GetArea() float64 {
	return r.area
}

type triangle struct {
	height float64
	bottom float64
	area   float64
}

func (t *triangle) CalArea() {
	area := t.bottom * t.height / 2
	t.area = area
}

func (t *triangle) GetArea() float64 {
	return t.area
}

type rectangle struct {
	height float64
	width  float64
	area   float64
}

func (r *rectangle) CalArea() {
	area := r.height * r.width
	r.area = area
}

func (r *rectangle) GetArea() float64 {
	return r.area
}

// 图形排序
func SortByAreaDesc(shapes ...shaper) {
	// 计算面积
	for _, shape := range shapes {
		shape.CalArea()
	}

	var j1, j2 float64
	// 升序排序面积
	// 利用冒泡排序来对 shapes 切片进行排序
	for i := 0; i < len(shapes); i++ {
		for j := 0; j < len(shapes)-i-1; j++ {
			j1 = shapes[j].GetArea()
			j2 = shapes[j+1].GetArea()

			if j1 > j2 {
				shapes[j], shapes[j+1] = shapes[j+1], shapes[j]
			}
		}
	}

	// 输出排序后的内容
	for _, v := range shapes {
		fmt.Print(v.GetArea(), " ")
	}
}

func main() {
	// 创建一个圆形
	r1 := new(round)
	r1.radius = 2.1
	// fmt.Printf("round -> %p\n", r1)

	// 创建一个三角形
	tr1 := new(triangle)
	tr1.bottom = 2.5
	tr1.height = 3.0
	// fmt.Printf("triangle -> %p\n", tr1)

	// 创建一个长方形
	t1 := new(rectangle)
	t1.width = 2.0
	t1.height = 3.0
	// fmt.Printf("rectangle -> %p\n", t1)

	t2 := new(rectangle)
	t2.width = 3.0
	t2.height = 3.0

	SortByAreaDesc(r1, tr1, t1, t2)
}
